package com.umeox.watch.moto.gallery.provider;

import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.umeox.watch.moto.gallery.bean.Gallery;
import com.umeox.watch.moto.gallery.db.AppDataBase;
import com.umeox.watch.moto.gallery.db.model.AlbumInfo;

/**
 * Created by zl on 2017/6/6.
 */

public class GalleryProvider extends ContentProvider {

    private static final String TAG = "GalleryProvider";

    private static final String DB_NAME = "gallery.db";
    private static final String DB_TABLE = AlbumInfo.TABLE_NAME;

    private static final int GALLERY_INFO_CONTENT_URI = 0;


    private final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);



    @Override
    public boolean onCreate() {
        MATCHER.addURI(Gallery.AUTHORITY, Gallery.PATH, GALLERY_INFO_CONTENT_URI);
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return null;
        }
        String sql = SQLiteQueryBuilder.buildQueryString(false
                , AlbumInfo.TABLE_NAME
                , projection
                , selection
                , null
                , null
                , sortOrder
                , null);

        return AppDataBase.getInstance().query(sql, selectionArgs);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        String type;
        switch (MATCHER.match(uri)) {
            case GALLERY_INFO_CONTENT_URI:
                type = "vnd.android.cursor.dir/" + AlbumInfo.TABLE_NAME;
                break;
            default:
                throw new IllegalArgumentException("unknown uri:" + uri);
        }
        return type;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return null;
        }

        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();

        final long rowId = writableDatabase.insert(AlbumInfo.TABLE_NAME, CONFLICT_REPLACE, values);
        if (rowId >= 0) {
            //getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, rowId);
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return 0;
        }
        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();
        final int count = writableDatabase.delete(AlbumInfo.TABLE_NAME, selection, selectionArgs);
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        final int code = MATCHER.match(uri);
        if (code < 0) {
            return 0;
        }
        final AppDataBase dataBase = AppDataBase.getInstance();
        final SupportSQLiteDatabase writableDatabase = dataBase.getOpenHelper().getWritableDatabase();
        final int count = writableDatabase.update(AlbumInfo.TABLE_NAME, 0, values, selection, selectionArgs);
        return count;
    }

}
